package io.renren.modules.sys.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.AgentDao;
import io.renren.modules.sys.service.AgentService;


@Service("agentService")
public class AgentServiceImpl extends ServiceImpl<AgentDao, AgentEntity> implements AgentService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleService sysRoleService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String username = null;
        if( params.get("username")!=null&&!params.get("username").equals("")){//代理商名称
            username =params.get("username").toString();
        }
        String mobile = null;
        if( params.get("mobile")!=null&&!params.get("mobile").equals("")){//代理商手机号
            mobile =params.get("mobile").toString();
        }

        int count = baseMapper.queryListcount(username,mobile,user.getUserId());
        List<SysUserEntity> list = baseMapper.getAgentList(username,mobile,user.getUserId(),(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public PageUtils queryBusinessPage(Map<String, Object> params) {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        int pageSize = Integer.parseInt(params.get("limit").toString());  //每页记录数
        int currPage = Integer.parseInt(params.get("page").toString());  //当前页数
        String username = null;
        if( params.get("username")!=null&&!params.get("username").equals("")){//代理商名称
            username =params.get("username").toString();
        }
        String mobile = null;
        if( params.get("mobile")!=null&&!params.get("mobile").equals("")){//代理商手机号
            mobile =params.get("mobile").toString();
        }

        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        String role = "";
        if(roleId==null) {
            role = "管理员";
        }
        int count = baseMapper.queryBusinessListcount(username,mobile,user.getUserId(),role);
        List<BusinessEntity> list = baseMapper.getBusinessList(username,mobile,user.getUserId(),role,(currPage-1)*pageSize,currPage*pageSize);
        return new PageUtils(list,count,pageSize,currPage);
    }

    @Override
    public int queryLineCodeNum(Integer userid){
        int num = baseMapper.queryLineCodeNum(userid);
        return num;
    }

    @Override
    public int querySaleLineCodeNum(Integer userid){
        int num = baseMapper.querySaleLineCodeNum(userid);
        return num;
    }

    @Override
    public SysUserEntity queryAgentBySaleid(Integer saleid){
        SysUserEntity entity = baseMapper.queryAgentBySaleid(saleid);
        return entity;
    }

}
